package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.ui.activity.ACT_AddOrModifyBaby;
import cn.mumzone.pos.ui.activity.ACT_MemberDetail;

public class ADA_Member_Baby extends RecyclerView.Adapter<ADA_Member_Baby.ViewHolder> {


    private List<MemberEntity.BabysBean> babies;

    private Context context;

    private String memberId;

    public ADA_Member_Baby(String memberId) {
        this.memberId = memberId;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_member_baby_info, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final MemberEntity.BabysBean baby = babies.get(position);
        holder.tvBabyName.setText(baby.getName());
        if (baby.getGender() == 0) {//男
            holder.ivBabySex.setImageResource(R.drawable.svg_ic_male);
        } else {
            holder.ivBabySex.setImageResource(R.drawable.svg_ic_female);
        }

        //计算生日到现在几年几个月几天
        holder.tvBabyBirth.setText(AppDateUtil.dayComparePrecise(baby.getBirthday()));

        holder.ivModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ACT_AddOrModifyBaby.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("baby", baby);
                bundle.putString("memberId", memberId);
                intent.putExtras(bundle);
                ((AppCompatActivity) context).startActivityForResult(intent, ACT_MemberDetail.REQUEST_CODE_MODIFY_BABY);
            }
        });
    }

    @Override
    public int getItemCount() {
        return babies == null ? 0 : babies.size();
    }


    public void setData(List<MemberEntity.BabysBean> babies) {
        this.babies = babies;
    }

    public void appendData(MemberEntity.BabysBean baby) {
        if (babies == null) {
            babies = new ArrayList<>();
        }
        babies.add(baby);
        notifyDataSetChanged();
    }

    public List<MemberEntity.BabysBean> getData() {
        return babies;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_baby_name)
        TextView tvBabyName;
        @BindView(R.id.iv_baby_sex)
        AppCompatImageView ivBabySex;
        @BindView(R.id.tv_baby_birth)
        TextView tvBabyBirth;
        @BindView(R.id.iv_modify)
        AppCompatImageView ivModify;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
